package engine;

import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Logfile {
	private /*@ spec_public @*/ String[] buffer;
	private /*@ spec_public @*/ String header;
	private /*@ spec_public @*/ int bufMax;
	private /*@ spec_public @*/ int bufIndex;
	private /*@ spec_public @*/ File file;
	private String newLine;
	/**
	 * Logfile Constructor
	 * @param bufMax the maximum number of strings allowed in the buffer before the log is written
	 */
	/*
	 * @assignable this.bufMax, buffer, bufIndex, path;
	 * @requires bufMax > 0;
	 * @ensures this.bufMax == bufMax;
	 * @ensures buffer != null;
	 * @ensures bufIndex == 0;
	 * @ensures path != null;
	 */
	public Logfile(int bufMax)
	{
		if(bufMax<=0){System.out.println("Illegal Input, cannot have negative or zero sized buffer");return;}
		this.bufMax = bufMax;
		newLine = System.getProperty("line.separator").toString();
		buffer = new String[bufMax];
		header = generateHeader();
		bufIndex = 0;
		try {
			file = new File("Khushali_Logfile.txt");
			
			boolean success = file.createNewFile();
			if(success) {
				
			}
			else {
				file.delete();
				file.createNewFile();
				try {
					BufferedWriter out = new BufferedWriter(new FileWriter("Khushali_Logfile.txt", true));
				    out.write(header);
				    out.close();
					} 
				catch (IOException e) {
					System.err.println(e.getMessage());
				}
			}
		}
		catch(IOException e) {
			System.err.println("Error in creating new logfile\n");	
			return;
		}
	}
	/**import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
	 * addToBuffer appends a string to the Logfile's buffer
	 * @param s the string to append to the buffer
	 */
	/*
	 * @assignable buffer[bufIndex];
	 * @requires s != null;
	 * @ensures bufIndex == (\old(bufIndex)+1)%bufMax;
	 * @ensures buffer[bufIndex] == s;
	 */
	public void addToBuffer(String s)
	{
		if(s != null) {
			buffer[bufIndex] = s;
			bufIndex++;
			if(bufIndex == bufMax) {
				writeFile();
				bufIndex = 0;
			}
		}
	}
	/**
	 * writeFile writes out the contents of the buffer to the LogFile
	 */
	/*
	 * @ assignable \nothing
	 * @ requires s != null;
	 */
	public void writeFile()
	{
		try {
		    BufferedWriter out = new BufferedWriter(new FileWriter("Khushali_Logfile.txt", true));
		    for(String b : buffer) {
			    out.write(b);
		    }
		    out.close();
			} 
		catch (IOException e) {
			System.err.println(e.getMessage());
		}
	
	}
	/**
	 * setBufMax sets the maximum number of strings allowed in the buffer at one time 
	 * @param bufMax the number of strings to allow in the buffer before the file is written
	 */
	/*
	 * @assignable this.bufMax;
	 * @assignable buffer;
	 * @requires bufMax > 0;
	 * @ensures this.bufMax == bufMax;
	 * @ensures buffer.size == bufMax;
	 */
	public void setBufMax(int bufMax) {
		if(bufMax<=0){System.out.println("Illegal Input, cannot have negative or zero sized buffer");return;}
		this.bufMax = bufMax;
		buffer = new String[bufMax];
	}
	/**
	 * generateHeader generates a header for the output
	 * @return the string containing the header
	 */
	/*
	 * @requires this!=null;
	 * @returns String;
	 */
	private String generateHeader() {
		String ret = "";
		ret += "Khushali Logfile " + getDateTime() + newLine;
		ret += "all you base are belong to us" + newLine;
		return ret;
	}
	/**
	 * getDateTime returns a string representation of the system time
	 * @return the string representing the system time
	 */
	/*
	 * @requires this!=null
	 * @returns String;
	 */
	private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

}
